package com.example.demo.service;

import com.example.demo.mapper.PlatAreaMapper;
import com.example.demo.model.PlatArea;
import com.github.pagehelper.PageHelper;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class PlatAreaService {

    @Autowired
    private PlatAreaMapper platAreaMapper;
 
    public PlatArea selectOne(Integer id){
    	PlatArea model = new PlatArea();
    	model.setId(id);
        return platAreaMapper.selectOne(model);
    }
    
    public List<PlatArea> getCities(){
        //单表自定义查询
        Example example = new Example(PlatArea.class);
        Criteria criteria = example.createCriteria();
        criteria.andIsNull("parentId");
 
        return platAreaMapper.selectByExample(example);
    }
    
    public List<PlatArea> getDistricts(Integer parentId){
        //单表自定义查询
        Example example = new Example(PlatArea.class);
        Criteria criteria = example.createCriteria();
        criteria.andEqualTo("parentId", parentId);
 
        return platAreaMapper.selectByExample(example);
    }

    
    public List<PlatArea> selectAll(Integer pageIndex,Integer pageSize){
        // 单表分页
        PageHelper.startPage(pageIndex,pageSize,false);

        //单表自定义查询
        //Example example = new Example(Demo.class);

        return platAreaMapper.selectAll();
    }

    public List<PlatArea> selectAll(String filter,Integer pageIndex,Integer pageSize){
        // 单表分页
        PageHelper.startPage(pageIndex,pageSize);
        Example example = new Example(PlatArea.class);
        if(StringUtils.isNotEmpty(filter)) {
            filter = "%" + filter + "%";
            example.createCriteria().andLike("name", filter);
        }

        return platAreaMapper.selectByExample(example);
    }

    public PlatArea createOrUpdate(PlatArea model) {
        // 新增
        if(model.getId() == null){
        	platAreaMapper.insert(model);
        }else{
        	platAreaMapper.updateByPrimaryKeySelective(model);
        }
        return model;
    }

    public int delete(Integer id) {
    	PlatArea model = new PlatArea();
        model.setId(id);
        return platAreaMapper.deleteByPrimaryKey(model);
    }

}
